package com.suyang.syt.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.baidumap.AddressComponent;
import com.suyang.syt.model.dto.baidumap.BaiduMapResult;
import com.suyang.syt.model.dto.baidumap.Location;
import com.suyang.syt.model.dto.baidumap.Result;

public class BaiduMapUtil {
	private static final String BAIDUURL = "http://api.map.baidu.com/geocoder/v2/";
	private static final String AK = "u8RjGZIpmW6RNE7xR7GeVjVq";

	/**
	 * 根据地址信息获取该地址的详细信息,经纬度等
	 * 
	 * @param address
	 *            地址字符串
	 * @param city
	 *            城市字符串,该参数是可选项,用于指定上述地址所在的城市,当多个城市都有上述地址时,该参数起到过滤作用
	 * @return
	 * @throws Exception
	 */
	private static BaiduMapResult getLngAndLat(String address, String city) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ak", AK);
		map.put("output", "json");
		map.put("address", address);
		map.put("city", city);
		String result = HttpTookit.doGet(BAIDUURL, map);
		return JsonUtil.fromJson(result, BaiduMapResult.class);
	}

	/**
	 * 根据经纬度获取地址信息,默认pois==0时候不显示poi信息 ==1时候显示poi信息
	 * 
	 * @param lat
	 *            纬度
	 * @param lng
	 *            经度
	 * @param pois
	 *            为0不显示,为1显示
	 * @return
	 * @throws Exception
	 */
	private static BaiduMapResult getAddress(double lat, double lng, int pois) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ak", AK);
		map.put("output", "json");
		map.put("location", lat + "," + lng);
		if (pois == 1) {
			map.put("pois", String.valueOf(pois));
		}
		String result = HttpTookit.doGet(BAIDUURL, map);
		return JsonUtil.fromJson(result, BaiduMapResult.class);
	}

	/**
	 * 根据link地址发送URL请求,返回json字符串信息
	 * 
	 * @param link
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private static String loadJsonData(String link) throws Exception {
		StringBuilder json = new StringBuilder();
		URL url = new URL(link);
		URLConnection connection = url.openConnection();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String inputLine = null;
		while ((inputLine = bufferedReader.readLine()) != null) {
			json.append(inputLine);
		}
		bufferedReader.close();
		return json.toString();
	}

	public static Map<String, String> findArea(double lat, double lng) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		BaiduMapResult baiduMapResult = getAddress(lat, lng, 0);
		String msg = "";
		if (baiduMapResult != null) {
			int status = baiduMapResult.getStatus();
			switch (status) {
			case 0:
				// 正常
				Result result = baiduMapResult.getResult();
				if (result != null) {
					AddressComponent address = result.getAddressComponent();
					if (address != null) {
						if (EmptyUtil.isNotEmpty(address.getProvince())) {
							map.put("province", address.getProvince());
						}
						if (EmptyUtil.isNotEmpty(address.getCity())) {
							map.put("city", address.getCity());
						}
						if (EmptyUtil.isNotEmpty(address.getDistrict())) {
							map.put("district", address.getDistrict());
						}
					}
				}
				msg = "百度API获取地址信息成功!";
				break;
			default:
				msg = "您所填写的经纬度不正确!";
				break;
			}
		}
		map.put("msg", msg);
		return map;
	}

	public static AjaxObj findLatLng(String address, String city) throws Exception {
		BaiduMapResult baiduMapResult = getLngAndLat(address, city);
		String msg = "";
		if (baiduMapResult != null) {
			int status = baiduMapResult.getStatus();
			switch (status) {
			case 0:
				// 正常
				Result result = baiduMapResult.getResult();
				if (result != null) {
					Location location = result.getLocation();
					if (location != null) {
						msg = "百度API获取地址经纬度成功!";
						return new AjaxObj(1, msg, location);
					}
				}
				break;
			default:
				msg = "您所填写的地址信息不正确!";
				break;
			}
		}
		return new AjaxObj(0, msg);
	}

}
